package com.fr.base.file;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.XMLFileManager;
import com.fr.base.core.util.ListMap;
import com.fr.base.xml.XMLObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary;
import com.fr.data.TableData;
import com.fr.data.condition.FalseCondition;
import com.fr.data.core.DataXMLUtils;
import com.fr.data.impl.Connection;
import com.fr.dialog.NameObject;
import com.fr.report.io.xml.ReportDeprecatedXMLUtils;
import com.fr.report.io.xml.SynchronizedVersion;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DatasourceManager extends XMLFileManager
{
  private ListMap nameConnectionMap = new ListMap();
  private ListMap nameTableDataMap = new ListMap();
  private ListMap nameADHOCMap = new ListMap();
  private List nameCondition = null;
  private DeprecatedDictionaryManager deprecatedDicManager = new DeprecatedDictionaryManager();
  private String scheduleDatasource = null;

  public String fileName()
  {
    return "datasource.xml";
  }

  public List getNameCondition()
  {
    return this.nameCondition;
  }

  public void setNameCondition(FalseCondition paramFalseCondition)
  {
    if (this.nameCondition == null)
      this.nameCondition = new ArrayList();
    this.nameCondition.add(paramFalseCondition);
  }

  public Iterator getConnectionNameIterator()
  {
    return this.nameConnectionMap.keySet().iterator();
  }

  public Object[] getConnectionNameArray()
  {
    return this.nameConnectionMap.keySet().toArray();
  }

  public Connection getConnection(String paramString)
  {
    return ((Connection)this.nameConnectionMap.get(paramString));
  }

  public void putConnection(String paramString, Connection paramConnection)
  {
    if (paramConnection == null)
      this.nameConnectionMap.remove(paramString);
    this.nameConnectionMap.put(paramString, paramConnection);
  }

  public void removeConnection(String paramString)
  {
    this.nameConnectionMap.remove(paramString);
  }

  public void clearAllConnection()
  {
    this.nameConnectionMap.clear();
  }

  public Iterator getTableDataNameIterator()
  {
    return this.nameTableDataMap.keySet().iterator();
  }

  private TableData getStoredTableData(String paramString)
  {
    return ((TableData)this.nameTableDataMap.get(paramString));
  }

  public TableData getTableData(String paramString)
  {
    TableData localTableData = getStoredTableData(paramString);
    try
    {
      return ((localTableData == null) ? null : (TableData)localTableData.clone());
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
    }
    return null;
  }

  public void putTableData(String paramString, TableData paramTableData)
  {
    if (paramTableData == null)
      this.nameTableDataMap.remove(paramString);
    this.nameTableDataMap.put(paramString, paramTableData);
  }

  public boolean renameTableData(String paramString1, String paramString2)
  {
    if ((!(this.nameTableDataMap.containsKey(paramString1))) || (this.nameTableDataMap.containsKey(paramString2)))
      return false;
    int i = this.nameTableDataMap.indexOf(paramString1);
    this.nameTableDataMap.put(i, paramString2, this.nameTableDataMap.get(paramString1));
    return true;
  }

  public void removeTableData(String paramString)
  {
    this.nameTableDataMap.remove(paramString);
  }

  public void clearAllTableData()
  {
    this.nameTableDataMap.clear();
  }

  public Iterator getADHOCNameIterator()
  {
    return this.nameADHOCMap.keySet().iterator();
  }

  private TableData getStoredADHOC(String paramString)
  {
    return ((TableData)this.nameADHOCMap.get(paramString));
  }

  public TableData getADHOC(String paramString)
  {
    TableData localTableData = getStoredADHOC(paramString);
    try
    {
      return ((localTableData == null) ? null : (TableData)localTableData.clone());
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
    }
    return null;
  }

  public void putADHOC(String paramString, TableData paramTableData)
  {
    if (paramTableData == null)
      this.nameADHOCMap.remove(paramString);
    this.nameADHOCMap.put(paramString, paramTableData);
  }

  public boolean renameADHOC(String paramString1, String paramString2)
  {
    if ((!(this.nameADHOCMap.containsKey(paramString1))) || (this.nameADHOCMap.containsKey(paramString2)))
      return false;
    int i = this.nameADHOCMap.indexOf(paramString1);
    this.nameADHOCMap.put(i, paramString2, this.nameADHOCMap.get(paramString1));
    return true;
  }

  public void removeADHOC(String paramString)
  {
    this.nameADHOCMap.remove(paramString);
  }

  public void clearAllADHOC()
  {
    this.nameADHOCMap.clear();
  }

  public DeprecatedDictionaryManager getDeprecatedDictionaryManager()
  {
    return this.deprecatedDicManager;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
    {
      clearAllConnection();
      clearAllTableData();
      this.deprecatedDicManager.clearAllDictionary();
    }
    if (paramXMLableReader.isChildNode())
    {
      String str1 = paramXMLableReader.getTagName();
      if (("ConnectionMap".equals(str1)) || ("DatasourceMap".equals(str1)))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if (paramXMLableReader.isChildNode())
            {
              String str1 = paramXMLableReader.getTagName();
              if (("Connection".equals(str1)) || ("Datasource".equals(str1)))
              {
                String str2;
                Object localObject = null;
                if ((str2 = paramXMLableReader.getAttr("name")) != null)
                  localObject = str2;
                if (localObject == null)
                  return;
                Connection localConnection = DataXMLUtils.readXMLConnection(paramXMLableReader);
                if (localConnection != null)
                  this.this$0.putConnection(localObject, localConnection);
              }
            }
          }
        });
      }
      else if ("TableDataMap".equals(str1))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("TableData")))
            {
              Object localObject = null;
              String str = null;
              if ((str = paramXMLableReader.getAttr("name")) != null)
                localObject = str;
              if (localObject == null)
                return;
              TableData localTableData = DataXMLUtils.readXMLTableData(paramXMLableReader);
              if (localTableData != null)
                this.this$0.putTableData(localObject, localTableData);
            }
          }
        });
      }
      else if ("ADHOCMap".equals(str1))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("TableData")))
            {
              Object localObject = null;
              String str = null;
              if ((str = paramXMLableReader.getAttr("name")) != null)
                localObject = str;
              if (localObject == null)
                return;
              TableData localTableData = DataXMLUtils.readXMLTableData(paramXMLableReader);
              if (localTableData != null)
                this.this$0.putADHOC(localObject, localTableData);
            }
          }
        });
      }
      else if (("DictMap".equals(str1)) || ("MapMap".equals(str1)))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if (paramXMLableReader.isChildNode())
            {
              String str1 = paramXMLableReader.getTagName();
              if ((str1.equals("Dictionary")) || (str1.equals("Dict")) || (str1.equals("Map")))
              {
                Object localObject = null;
                String str2 = null;
                if ((str2 = paramXMLableReader.getAttr("name")) != null)
                  localObject = str2;
                if (localObject == null)
                  return;
                Dictionary localDictionary = DataXMLUtils.readXMLDictionary(paramXMLableReader);
                if (localDictionary != null)
                  DatasourceManager.access$000(this.this$0).putDictionary(localObject, localDictionary);
              }
            }
          }
        });
      }
      else if ("Relations".equals(str1))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && ("Relation".equals(paramXMLableReader.getTagName())))
            {
              String str;
              Object localObject1 = "";
              Object localObject2 = "";
              Object localObject3 = "";
              Object localObject4 = "";
              if ((str = paramXMLableReader.getAttr("selfData")) != null)
                localObject1 = str;
              if ((str = paramXMLableReader.getAttr("selfField")) != null)
                localObject2 = str;
              if ((str = paramXMLableReader.getAttr("fatherData")) != null)
                localObject3 = str;
              if ((str = paramXMLableReader.getAttr("fatherField")) != null)
                localObject4 = str;
              FalseCondition localFalseCondition = new FalseCondition((String)localObject1, (String)localObject2, (String)localObject3, (String)localObject4);
              DatasourceManager.access$100(this.this$0).add(localFalseCondition);
            }
          }
        });
      }
      else if ("ScheduleDatasource".equals(str1))
      {
        setScheduleDatasource(paramXMLableReader.getElementValue());
      }
      else if ("Databases".equals(str1))
      {
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DatasourceManager this$0;

          public void readXML()
          {
            if ((paramXMLableReader.isChildNode()) && (paramXMLableReader.getTagName().equals("Database")))
            {
              String str = null;
              DatasourceManager.7 local7 = new DatasourceManager.7(this, null);
              paramXMLableReader.readXMLObject(local7);
              if ((str = (String)local7.getObject()) != null)
              {
                Connection localConnection = DataXMLUtils.readXMLConnection(paramXMLableReader);
                this.this$0.putConnection(str, localConnection);
              }
            }
          }
        });
      }
      else
      {
        Object localObject;
        if (str1.equals("NamedDatasource"))
        {
          localObject = ReportDeprecatedXMLUtils.readNamedDatasource_2006_02(paramXMLableReader);
          if (localObject != null)
            this.nameTableDataMap.put(((NameObject)localObject).getName(), ((NameObject)localObject).getObject());
        }
        else if (str1.equals("XMLVersion"))
        {
          String str2;
          if ((str2 = paramXMLableReader.getElementValue()) != null)
          {
            localObject = SynchronizedVersion.getSynchronizedVersion(Thread.currentThread());
            ((SynchronizedVersion)localObject).setXmlVersionByString(str2);
          }
        }
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    label72: Object localObject3;
    Object localObject4;
    paramXMLPrintWriter.startTAG("DatasourceManager");
    Iterator localIterator = getConnectionNameIterator();
    if (localIterator.hasNext())
    {
      paramXMLPrintWriter.startTAG("ConnectionMap");
      while (true)
      {
        while (true)
        {
          if (!(localIterator.hasNext()))
            break label72;
          localObject1 = (String)localIterator.next();
          localObject2 = getConnection((String)localObject1);
          if (localObject2 != null)
            break;
        }
        DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, (Connection)localObject2, (String)localObject1);
      }
      paramXMLPrintWriter.end();
    }
    Object localObject1 = getTableDataNameIterator();
    if (((Iterator)localObject1).hasNext())
    {
      paramXMLPrintWriter.startTAG("TableDataMap");
      while (true)
      {
        while (true)
        {
          if (!(((Iterator)localObject1).hasNext()))
            break label145;
          localObject2 = (String)((Iterator)localObject1).next();
          localObject3 = getStoredTableData((String)localObject2);
          if (localObject3 != null)
            break;
        }
        DataXMLUtils.writeXMLTableData(paramXMLPrintWriter, (TableData)localObject3, (String)localObject2);
      }
      label145: paramXMLPrintWriter.end();
    }
    Object localObject2 = getADHOCNameIterator();
    if (((Iterator)localObject2).hasNext())
    {
      paramXMLPrintWriter.startTAG("ADHOCMap");
      while (true)
      {
        while (true)
        {
          if (!(((Iterator)localObject2).hasNext()))
            break label222;
          localObject3 = (String)((Iterator)localObject2).next();
          localObject4 = getStoredADHOC((String)localObject3);
          if (localObject4 != null)
            break;
        }
        DataXMLUtils.writeXMLTableData(paramXMLPrintWriter, (TableData)localObject4, (String)localObject3);
      }
      label222: paramXMLPrintWriter.end();
    }
    if ((this.nameCondition != null) && (this.nameCondition.size() > 0))
    {
      paramXMLPrintWriter.startTAG("Relations");
      for (int i = 0; i < this.nameCondition.size(); ++i)
      {
        localObject4 = (FalseCondition)this.nameCondition.get(i);
        paramXMLPrintWriter.startTAG("Relation").attr("selfData", ((FalseCondition)localObject4).getSelfData()).attr("selfField", ((FalseCondition)localObject4).getSelfField()).attr("fatherData", ((FalseCondition)localObject4).getFatherData()).attr("fatherField", ((FalseCondition)localObject4).getFatherField()).end();
      }
      paramXMLPrintWriter.end();
    }
    if (StringUtils.isNotEmpty(this.scheduleDatasource))
      paramXMLPrintWriter.startTAG("ScheduleDatasource").textNode(this.scheduleDatasource).end();
    paramXMLPrintWriter.end();
  }

  public String getScheduleDatasource()
  {
    return this.scheduleDatasource;
  }

  public void setScheduleDatasource(String paramString)
  {
    this.scheduleDatasource = paramString;
  }

  static DeprecatedDictionaryManager access$000(DatasourceManager paramDatasourceManager)
  {
    return paramDatasourceManager.deprecatedDicManager;
  }

  static List access$100(DatasourceManager paramDatasourceManager)
  {
    return paramDatasourceManager.nameCondition;
  }
}